package com.qdxwx.data.service.statistic;


import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.data.mapper.statistic.StatisticMapper;
import com.qdxwx.models.statistic.Statistic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * Statistic接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class StatisticServiceImpl implements StatisticService {

	private static final Logger logger = LoggerFactory.getLogger(StatisticServiceImpl.class);

	@Autowired
	private StatisticMapper statisticMapper;

	/**
	 * 定时添加StatisticDay
	 */
	@Override
	public void autoCreateYesterday() {
		//每天定时统计前一天的数据，天数减1
		LocalDate date = LocalDate.now().plusDays(-1);

		autoCreateDay(date);

		logger.info("定时生成统计数据 :" + date + "的数据");

		//定时清理4个月前的后台操作日志
		//LocalDate clearDate = LocalDate.now().plusDays(-120);
		//清理

		//定时清理4个月前的后台登录日志

		//logger.info("定时清理4个月前的后台操作日志");
		//定时清理4个月前的后台操作日志
	}

	@Override
	public void autoCreateDay(LocalDate date) {
		//先删除再生成，防止数据重复
		statisticMapper.delete(date);
		//添加网校统计
		statisticMapper.insert(date);
	}

	/**
	 * 按月查询网站统计
	 */
	@Override
	public List<Statistic> getListByMonth(int year, int month) {
		return statisticMapper.selectListByMonth(year, month);
	}

	/**
	 * 按年查询网站统计
	 */
	@Override
	public List<Statistic> sumMonthsByYear(int year) {
		return statisticMapper.sumMonthsByYear(year);
	}

	/**
	 * 网站统计 （总记录）
	 */
	@Override
	public Statistic sumTotal() {
		Statistic statistic = (Statistic) CacheUtils.get(CacheConstants.WEB_STATISTICS_PREFIX + "site");
		if (statistic != null) {
			return statistic;
		}
		statistic = statisticMapper.sumTotal();
		if (statistic != null) {
			CacheUtils.set(CacheConstants.WEB_STATISTICS_PREFIX + "site", statistic);
		}
		return statistic;
	}

	/**
	 * 动态查询活跃度
	 */
	@Override
	public List<Statistic> getList(int days) {
		return statisticMapper.selectListByDays(days);
	}

	/**
	 * 查询指定时间段的统计数据
	 */
	@Override
	public List<Statistic> getList(LocalDate startTime, LocalDate endTime) {
		return statisticMapper.selectListByDate(startTime, endTime);
	}

	/**
	 * 删除指定时间段的统计数据
	 */
	@Override
	public void delete(LocalDate startDate, LocalDate endDate) {
		statisticMapper.deleteByDate(startDate, endDate);
		CacheUtils.remove(CacheConstants.WEB_STATISTICS_PREFIX + "*");
	}

	/**
	 * 获取日期的登录人数
	 */
	@Override
	public int countLoginNum(LocalDate date) {
		return statisticMapper.countLoginNum(date);
	}

	/**
	 * 获取日期的注册人数
	 */
	@Override
	public int countRegisterNum(LocalDate date) {
		return statisticMapper.countRegisterNum(date);
	}

	/**
	 * 获取日期的订单数
	 */
	@Override
	public Map<String, Object> getTodayOrderNum(LocalDate date) {
		Map<String, Object> map = new HashMap<>();
		map.put("orderNum", statisticMapper.countOrderNum(date));
		map.put("orderSuccessNum", statisticMapper.countOrderSuccessNum(date));
		map.put("orderInitNum", statisticMapper.countOrderInitNum(date));
		map.put("orderClosedNum", statisticMapper.countOrderClosedNum(date));
		return map;
	}

	/**
	 * 网校课程数
	 */
	@Override
	public int countCourseNum() {
		return statisticMapper.countCourseNum();
	}

	/**
	 * 网校总用户数
	 */
	@Override
	public int countUserNum() {
		return statisticMapper.countUserNum();
	}

	/**
	 * 每日收入(条件没有用上)
	 */
	@Override
	public int sumIncome(LocalDate startDate, LocalDate endDate) {
		return statisticMapper.sumIncome(startDate, endDate);
	}
}